package com.kyrie.lib_sys.application.autorApplys.controller;

import com.google.common.base.Preconditions;
import com.kyrie.lib_sys.common.entity.PageResult;
import com.kyrie.lib_sys.common.entity.Result;
import com.kyrie.lib_sys.common.pojo.dto.AuthorDTO;
import com.kyrie.lib_sys.common.pojo.dto.AuthorPageQueryDTO;
import com.kyrie.lib_sys.application.autorApplys.service.AuthorService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作者相关controller类
 *
 * @author heefM
 * @date 2025-08-07
 */
@RestController
@RequestMapping("/author/")
@Slf4j
public class AuthorController {

    @Autowired
    private AuthorService authorService;

    @RequestMapping("/add")
    public Result addAuthor(@RequestBody AuthorDTO authorDTO) {
        if (log.isInfoEnabled()) {
            log.info("添加作者Controller:addAuthor: {}", authorDTO);
        }
        Preconditions.checkArgument(!StringUtils.isBlank(authorDTO.getName()), "作者姓名不能为空");

        authorService.insert(authorDTO);
        return Result.success("添加作者成功");
    }

    /**
     * 根据id查询作者
     *
     * @param id
     * @return
     */
    @RequestMapping("/queryById")
    public Result<AuthorDTO> queryAuthorById(@RequestParam Long id) {
        log.info("根据id查询作者: {}", id);
        Preconditions.checkNotNull(id, "作者id不能为空");
        AuthorDTO authorDTO = authorService.queryAuthorById(id);

        return Result.success(authorDTO);
    }

    /**
     * 分页查询所有作者
     *
     * @return
     */
    @RequestMapping("/list")
    public Result<PageResult> queryPage(AuthorPageQueryDTO authorPageQueryDTO) {

        log.info("分页查询所有作者: {}", authorPageQueryDTO);
        // 设置默认分页参数
        if (authorPageQueryDTO.getPageNum() == null) {
            authorPageQueryDTO.setPageNum(1);
        }
        if (authorPageQueryDTO.getPageSize() == null) {
            authorPageQueryDTO.setPageSize(10);
        }
        PageResult pageResult = authorService.queryPage(authorPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 条件查询作者
     *
     * @param authorDTO
     * @return
     */
    @RequestMapping("/queryByCondition")
    public Result<AuthorDTO> queryByCondition(@RequestBody AuthorDTO authorDTO) {
        if (log.isInfoEnabled()) {
            log.info("条件查询作者: {}", authorDTO);
        }
        authorDTO = authorService.queryByCondition(authorDTO);
        return Result.success(authorDTO);
    }

    /**
     * 修改作者
     *
     * @param authorDTO
     * @return
     */
    @RequestMapping("/update")
    public Result updateAuthor(@RequestBody AuthorDTO authorDTO) {
        if (log.isInfoEnabled()) {
            log.info("修改作者Controller:updateAuthor: {}", authorDTO);
        }
        Preconditions.checkArgument(authorDTO != null, "作者信息不能为空");
        authorService.update(authorDTO);
        return Result.success("修改作者成功");
    }

    /**
     * 根据id删除作者
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result<String> deleteAuthor(@RequestParam("id") Long id) {
        if (log.isInfoEnabled()) {
            log.info("删除作者Controller:deleteAuthor: {}", id);
        }
        authorService.deleteById(id);
        return Result.success("删除作者成功");
    }

    /**
     * 批量删除作者
     *
     * @param ids
     * @return
     */
    @RequestMapping("/deleteBatch")
    public Result<String> deleteBatch(@RequestParam List<Long> ids) {
        if (log.isInfoEnabled()) {
            log.info("批量删除作者Controller:deleteBatch: {}", ids);
        }
        authorService.deleteBatch(ids);
        return Result.success("批量删除作者成功");
    }

    /**
     * 批量新增作者
     *
     * @param authorDTOS
     * @return
     */
    @RequestMapping("/insertBatch")
    public Result insertBatch(@RequestBody List<AuthorDTO> authorDTOS) {
        if (log.isInfoEnabled()) {
            log.info("批量新增作者insertBatch: {}", authorDTOS);
        }
        Preconditions.checkArgument(authorDTOS != null && authorDTOS.size() > 0, "作者列表不能为空");
        authorService.insertBatch(authorDTOS);
        return Result.success("批量新增作者成功");
    }
}
